package at.studentinformation;

import java.util.ArrayList;
import java.util.Date;
import org.json.JSONArray;
import org.json.JSONObject;

import android.database.Cursor;

public class News {
	public int Id;
	public String Title;
	public String Content;
	public int Hot;
    public int CategoryId;
    public Date Date;
    public boolean IsRead = false;
    
    //static DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
    //temp = formatter.format(result.Date);
    
    public static News ExamineFromJsonObject(JSONObject post)
	{
    	News result = new News();
		try
        {            
            result.Id = Integer.parseInt(post.getString("Id"));
            result.Title = post.getString("Title");
            result.Content = post.getString("Content");
            result.Hot = Integer.parseInt(post.getString("Hot"));
            result.CategoryId = Integer.parseInt(post.getString("CategoryId"));            
            
            String temp = post.getString("Date");
            temp = temp.substring(6, temp.length()-2);            
            result.Date = new Date(Long.parseLong(temp));
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
        }
		return result;
	}
	
	public static ArrayList<News> GetListSubjectFromJson(String input)
	{
		ArrayList<News> result = new ArrayList<News>();
		try
        {   			
            JSONArray entries = new JSONArray(input);
            for(int i =0; i<entries.length(); i++)
            {
            	JSONObject post = entries.getJSONObject(i);
            	News temp = News.ExamineFromJsonObject(post);
	            result.add(temp);
            }
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
        }
		return result;	
	}

	public static News GetNewsByCusor(Cursor c)
	{
		News result = new News();
		result.Title = c.getString(c.getColumnIndex("Title"));
		result.Content = c.getString(c.getColumnIndex("Content"));
		result.Id = Integer.parseInt(c.getString(c.getColumnIndex("Id")));
		result.Hot = Integer.parseInt(c.getString(c.getColumnIndex("Hot")));
		result.CategoryId = Integer.parseInt(c.getString(c.getColumnIndex("CategoryId")));
		result.Date = new Date(Long.parseLong(c.getString(c.getColumnIndex("Date"))));
		result.IsRead = Boolean.parseBoolean(c.getString(c.getColumnIndex("IsRead")));
		
		return result;
	}

	public static ArrayList<News> GetListNewsFormCursor(Cursor c)
	{
		ArrayList<News> result = new ArrayList<News>();
		while(c.moveToNext())
		{
			News temp = News.GetNewsByCusor(c);
			result.add(temp);
		}
		return result;
	}
}
